package com.yinhai.message.implement.app.adapter;

import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import com.yinhai.message.api.bean.TMUser;
import com.yinhai.message.api.util.MessageType;
import com.yinhai.message.api.vo.TMMessageVo;
import com.yinhai.message.core.adapterManager.IAdapter;
import com.yinhai.message.core.event.IMessageEvent;
import com.yinhai.message.core.exception.ResultsHandle;
import com.yinhai.message.implement.app.event.AppMessageEvent;
import com.yinhai.message.implement.app.sender.AppMessageSender;

/**
 * @author:TANQINGPING
 * @version:1.0 2017/3/31
 */
@Component("appAdapter")
public class AppAdapter implements IAdapter, ApplicationListener<AppMessageEvent> {
    private static final Logger LOGGER = LoggerFactory.getLogger(AppAdapter.class);

    @Resource(name = "resultsHandle")
    private ResultsHandle resultsHandle;

    @Resource(name = "appMessageSender")
    private AppMessageSender appMessageSender;
    
    @Override
    public String getSenderType() {
        return MessageType.APP;
    }

    @Override
    public IMessageEvent getEven(TMMessageVo vo) {
        return new AppMessageEvent(vo);
    }

    @Async
    @Override
    public void onApplicationEvent(AppMessageEvent appMessageEvent) {
    	LOGGER.debug("APP处理事件===============================================");
        TMMessageVo messageVo = appMessageEvent.getMessageVo();
        String mgid = messageVo.getMessage().getMgid();
        List<TMUser> resuleUserList = appMessageSender.sendMessage(messageVo);
        resultsHandle.handleResults(resuleUserList, mgid, getSenderType());
    }
}
